//
//  PopoverAnimator.swift
//  Weibo
//
//  Created by zxy on 2018/12/4.
//  Copyright © 2018 zxy. All rights reserved.
//

import UIKit

let popoverWillShow = "popoverWillShow"
let popoverWillDismiss = "popoverWillDismiss"

class PopoverAnimator: NSObject,UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning{
    
    //保存是否是展示
    var isPresent: Bool = false
    
    //接收外部传入的frame
    var popovetFrame: CGRect = CGRect.zero
    
    //MARK:-UIViewControllerTransitioningDelegate
    /// 初始化方法-用于创建负责转场动画的对象
    ///
    /// - Parameters:
    ///   - presented: 要展示的控制器
    ///   - presenting: 发起的控制器
    ///   - source: 源控制器
    /// - Returns: 负责转场动画的对象
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController?{//UIPresentationController：ios8.0专门负责转场动画
        let pc = PopoverPresentationController(presentedViewController: presented, presenting: presenting)
        pc.popovetFrame = popovetFrame
        return pc
    }
    //MARK:-实现了下面二个方法，所有的动画实现需程序员自己负责
    //告诉系统谁来负责展示动画
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?{
        isPresent = true
        
        //发送通知告诉控制器，已经展示了
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: popoverWillShow), object: self)
        
        return self
    }
    
    //告诉系统谁来负责modal的消失动画
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?{
        isPresent = false
        
        //发送通知告诉控制器，已经隐藏了
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: popoverWillDismiss), object: self)
        return self
    }
    
    //MARK:-UIViewControllerAnimatedTransitioning
    //返回动画的时长
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval{
        return 0.5
    }
    
    //执行动画方法,展示和隐藏都会走这个方法
    //transitionContext:动画的上下文，transitionContext保存了动画的所有属性
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning){
        if isPresent {//展示
            //1.获取要动画的View
            let toView = transitionContext.view(forKey: UITransitionContextViewKey.to)
            
            //2.将动画的View添加到容器视图里
            transitionContext.containerView.addSubview(toView!)
            
            //3.使用transform将view暂时隐藏
            toView?.transform = CGAffineTransform(scaleX: 1.0, y: 0.0)
            
            //设置动画锚点，默认是0.5，0.5（中心位置）
            //将锚点设置为0.5，0(从上往下展开)
            toView?.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
            
            UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
                //清空tranfrom
                toView?.transform = .identity
            }) { (_) in
                //动画执行完毕，一定要告诉系统，不然会导致一些未知错误
                transitionContext.completeTransition(true)
            }
        }else{//隐藏
            let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from)
            transitionContext.containerView.addSubview(fromView!)
            UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
                //0.0:因为CGFolat是不准确的，导致设置为0.0达不到效果，这里将其设置为一个很小的数
                fromView?.transform = CGAffineTransform(scaleX: 1.0, y: 0.00001)
            }) { (_) in
                transitionContext.completeTransition(true)
            }
        }
    }
}
